﻿{% if HasOperations -%}
{% if GenerateClientInterfaces -%}
{% if ExportTypes %}export {% endif %}interface I{{ Class }} {
{%     for operation in Operations -%}
    {% template Client.Method.Documentation %}
    {{ operation.MethodAccessModifier }}{{ operation.ActualOperationName }}({% for parameter in operation.Parameters %}{{ parameter.VariableName }}{% if GenerateOptionalParameters and parameter.IsOptional %}?{% endif %}: {{ parameter.Type }}{{ parameter.TypePostfix }}{% if parameter.IsLast == false%}, {% endif %}{% endfor %}): Observable<{{ operation.ResultType }}>;
{%     endfor -%}}
{%- endif -%}

{% if Framework.Angular.UseSingletonProvider -%}
@Injectable({
    providedIn: 'root'
})
{% else -%}
@Injectable()
{%- endif -%}
{% if ExportTypes %}export {% endif %}class {{ Class }} {% if HasBaseClass %}extends {{ BaseClass }} {% endif %}{% if GenerateClientInterfaces %}implements I{{ Class }} {% endif %}{
    private http: {{ Framework.Angular.HttpClass }};
    private baseUrl: string;
    protected jsonParseReviver: {% if SupportsStrictNullChecks %}((key: string, value: any) => any) | undefined{% else %}(key: string, value: any) => any{% endif %} = undefined;

{%- if HasExtendedConstructor == false -%}
    constructor({% if HasConfigurationClass %}@Inject({{ ConfigurationClass }}) configuration: {{ ConfigurationClass }}, {% endif %}@Inject({{ Framework.Angular.HttpClass }}) http: {{ Framework.Angular.HttpClass }}, @Optional() @Inject({{ Framework.Angular.BaseUrlTokenName }}) baseUrl?: string) {
{%-    if HasBaseClass -%}
        super({% if HasConfigurationClass %}configuration{% endif %});
{%-    endif -%}
        this.http = http;
{%-    if UseGetBaseUrlMethod -%}
        this.baseUrl = baseUrl ?? this.getBaseUrl("{{ BaseUrl }}");
{%-    else -%}
        this.baseUrl = baseUrl ?? "{{ BaseUrl }}";
{%-    endif -%}
    }
{%- endif -%}
{% if HasExtensionCode -%}

    {{ ExtensionCode }}
{%- endif -%}
{% for operation in Operations %}

    {% template Client.Method.Documentation %}
    {{ operation.MethodAccessModifier }}{{ operation.ActualOperationName }}({% for parameter in operation.Parameters %}{{ parameter.VariableName }}{% if GenerateOptionalParameters and parameter.IsOptional %}?{% endif %}: {{ parameter.Type }}{{ parameter.TypePostfix }}{% if parameter.IsLast == false or IncludeHttpContext == true%}, {% endif %}{% endfor %}{% if IncludeHttpContext%}httpContext?: HttpContext{% endif %}): Observable<{{ operation.ResultType }}> {
        {% template Client.RequestUrl %}

{%-    if operation.HasBody -%}
        {% template Client.RequestBody %}

{%-    endif -%}
        let options_ : any = {
{%-    if operation.HasBody -%}
            body: content_,
{%-    endif -%}
{%-    if Framework.Angular.UseHttpClient -%}
            observe: "response",
            responseType: "blob",
{%-       if Framework.Angular.WithCredentials -%}
            withCredentials: true,
{%-        endif -%}
{%-    else -%}
            method: "{{ operation.HttpMethodLower }}",
{%-    endif -%}
{%-    if operation.IsFile and Framework.Angular.UseHttpClient == false -%}
            responseType: ResponseContentType.Blob,
{%-     endif -%}
{%-     if IncludeHttpContext -%}
            context: httpContext,
{%-     endif -%}
            headers: new {% if Framework.Angular.UseHttpClient %}HttpHeaders{% else %}Headers{% endif %}({
{%-    for parameter in operation.HeaderParameters -%}
                "{{ parameter.Name }}": {{ parameter.VariableName }} !== undefined && {{ parameter.VariableName }} !== null ? "" + {{ parameter.VariableName }} : "",
{%-    endfor -%}
{%-    if operation.HasContent or operation.ConsumesFormUrlEncoded -%}
                "Content-Type": "{{ operation.Consumes }}",
{%-    endif -%}
{%-    if operation.HasResultType and operation.HasAcceptHeaderParameterParameter == false -%}
                "Accept": "{{ operation.Produces }}"
{%-    endif -%}
            })
        };

{%    if UseTransformOptionsMethod -%}
        return {{ Framework.RxJs.ObservableFromMethod }}(this.transformOptions(options_)).{% if Framework.UseRxJs6 %}pipe({% endif %}{{ Framework.RxJs.ObservableMergeMapMethod }}(transformedOptions_ => {
            return this.http.request({% if Framework.Angular.UseHttpClient %}"{{ operation.HttpMethodLower }}", {% endif %}url_, transformedOptions_);
        }){% if Framework.UseRxJs6 %}){% endif %}.{% if Framework.UseRxJs6 %}pipe({% endif %}{{ Framework.RxJs.ObservableMergeMapMethod }}((response_: any) => {
{%-    else -%}
        return this.http.request({% if Framework.Angular.UseHttpClient %}"{{ operation.HttpMethodLower }}", {% endif %}url_, options_).{% if Framework.UseRxJs6 %}pipe({% endif %}{{ Framework.RxJs.ObservableMergeMapMethod }}((response_ : any) => {
{%-    endif -%}
{%-    if UseTransformResultMethod -%}
            return this.transformResult(url_, response_, (r) => this.process{{ operation.ActualOperationNameUpper }}(r as any));
{%-    else -%}
            return this.process{{ operation.ActualOperationNameUpper }}(response_);
{%-    endif -%}
        }){% if Framework.UseRxJs6 %}){% endif %}.{% if Framework.UseRxJs6 %}pipe({% endif %}{{ Framework.RxJs.ObservableCatchMethod }}((response_: any) => {
            if (response_ instanceof {% if Framework.Angular.UseHttpClient %}HttpResponseBase{% else %}Response{% endif %}) {
                try {
{%-    if UseTransformResultMethod -%}
                    return this.transformResult(url_, response_, (r) => this.process{{ operation.ActualOperationNameUpper }}(r as any));
{%-    else -%}
                    return this.process{{ operation.ActualOperationNameUpper }}(response_ as any);
{%-    endif -%}
                } catch (e) {
                    return {{ Framework.RxJs.ObservableThrowMethod }}(e) as any as Observable<{{ operation.ResultType }}>;
                }
            } else
                return {{ Framework.RxJs.ObservableThrowMethod }}(response_) as any as Observable<{{ operation.ResultType }}>;
        }){% if Framework.UseRxJs6 %}){% endif %};
    }

    protected process{{ operation.ActualOperationNameUpper }}(response: {% if Framework.Angular.UseHttpClient %}HttpResponseBase{% else %}Response{% endif %}): Observable<{{ operation.ResultType }}> {
        const status = response.status;
{%- if Framework.Angular.UseHttpClient -%}
        const responseBlob =
            response instanceof HttpResponse ? response.body :
            (response as any).error instanceof Blob ? (response as any).error : undefined;
{%- endif -%}

        {% template Client.ProcessResponse %}
    }
{% endfor -%}
}
{%- endif -%}
